**** NOTE: REQUIRES st0085_2, vce2way, and boottest
* ssc install st0085_2 
* ssc install boottest
* ssc install vce2way


clear
clear mata
clear matrix
set more off
set matsize 11000
estimates drop _all

set seed 14850

cd "YOUR PATH HERE"
use "data/data_chen2013_wvs.dta", clear



******************************
** Code and Merge Languages **
******************************

gen language = ""
replace language = "Afrikaans" if g016 == 8
replace language = "Albanian" if g016 == 13
replace language = "Amharic" if g016 == 15
replace language = "Arabic" if g016 == 19
replace language = "Armenian" if g016 == 25
replace language = "Azari" if g016 == 39
replace language = "Azerbaijani" if g016 == 40
replace language = "Bambara" if g016 == 47
replace language = "Basque" if g016 == 52
replace language = "Belorussian" if g016 == 55
replace language = "Bemba" if g016 == 56
replace language = "Bengali" if g016 == 57
replace language = "Berber" if g016 == 58
replace language = "Bosnian" if g016 == 65
replace language = "Bulgarian" if g016 == 69
replace language = "Cantonese" if g016 == 73
replace language = "Catalan" if g016 == 75
replace language = "Croatian" if g016 == 90
replace language = "Czech" if g016 == 92
replace language = "Mandarin" if g016 == 101 | g016 == 102
replace language = "Dagbani" if g016 == 104
replace language = "Dyula" if g016 == 110
replace language = "Dutch" if g016 == 120
replace language = "English" if g016 == 128 | g016 == 129
replace language = "Estonian" if g016 == 132
replace language = "Ewe" if g016 == 135
replace language = "Persian" if g016 == 138
replace language = "Tagalog" if g016 == 140
replace language = "Finnish" if g016 == 141
replace language = "Fula" if g016 == 142
replace language = "French" if g016 == 144 | g016 == 145
replace language = "Ga" if g016 == 147
replace language = "Galician" if g016 == 151
replace language = "Gamo" if g016 == 154
replace language = "Georgian" if g016 == 157
replace language = "German" if g016 == 158
replace language = "Gamo" if g016 == 164
replace language = "Greek" if g016 == 166
replace language = "Gujarati" if g016 == 168
replace language = "Chaha" if g016 == 170
replace language = "Hakka" if g016 == 172
replace language = "Hausa" if g016 == 173
replace language = "Libido" if g016 == 174
replace language = "Hebrew" if g016 == 175
replace language = "Hindi" if g016 == 179
replace language = "Hungarian" if g016 == 184
replace language = "Igbo" if g016 == 190
replace language = "Indonesian" if g016 == 196
replace language = "Italian" if g016 == 207
replace language = "Japanese" if g016 == 208
replace language = "Javanese" if g016 == 209
replace language = "Kannada" if g016 == 220
replace language = "Kashmiri" if g016 == 225
replace language = "Kinyarwanda" if g016 == 232
replace language = "Kirghiz" if g016 == 236
replace language = "Korean" if g016 == 241
replace language = "Kurdish" if g016 == 247
replace language = "Latvian" if g016 == 256
replace language = "Lithuanian" if g016 == 261
replace language = "Luganda" if g016 == 268
replace language = "Macedonian" if g016 == 279
replace language = "Malay" if g016 == 285
replace language = "Malayalam" if g016 == 286
replace language = "Maltese" if g016 == 288
replace language = "Mandarin" if g016 == 289
replace language = "Maori" if g016 == 295
replace language = "Marathi" if g016 == 296
replace language = "Moldavian" if g016 == 309
replace language = "Montenegrin" if g016 == 313
replace language = "Mordvin" if g016 == 314
replace language = "Moore" if g016 == 315
replace language = "Arabic" if g016 == 316
replace language = "Sotho (Northern)" if g016 == 333
replace language = "Norwegian" if g016 == 334
replace language = "Chichewa" if g016 == 338
replace language = "Oromo" if g016 == 340
replace language = "Oriya" if g016 == 344
replace language = "Persian" if g016 == 358
replace language = "Polish" if g016 == 363
replace language = "Portuguese" if g016 == 364
replace language = "Panjabi" if g016 == 368
replace language = "Pashto" if g016 == 369
replace language = "Quechua" if g016 == 370
replace language = "Romanian" if g016 == 377
replace language = "Romani" if g016 == 378
replace language = "Russian" if g016 == 380
replace language = "Rutooro" if g016 == 381
replace language = "Wolof" if g016 == 396
replace language = "Serbian" if g016 == 397 | g016 == 398
replace language = "Seraiki" if g016 == 400
replace language = "Sidamo" if g016 == 402
replace language = "Sindhi" if g016 == 408
replace language = "Slovak" if g016 == 415
replace language = "Slovene" if g016 == 416
replace language = "Soddo" if g016 == 420
replace language = "Sesotho" if g016 == 422
replace language = "Spanish" if g016 == 426
replace language = "Sumatranese" if g016 == 429
replace language = "Sundanese" if g016 == 430
replace language = "Finnish" if g016 == 431
* Suomea is a case form of the Finnish name for Finnish *
replace language = "Swahili" if g016 == 433
replace language = "Swati" if g016 == 434
replace language = "Swedish" if g016 == 435
replace language = "Tagalog" if g016 == 437
replace language = "Mandarin" if g016 == 440
replace language = "Tamil" if g016 == 444
replace language = "Telugu" if g016 == 446
replace language = "Thai" if g016 == 447 | g016 == 448 | g016 == 449
replace language = "Tigrinya" if g016 == 450
replace language = "Thai" if g016 == 451
replace language = "Tsonga" if g016 == 462
replace language = "Tswana" if g016 == 463
replace language = "Turkish" if g016 == 466
replace language = "Akan" if g016 == 469
replace language = "Ukrainian" if g016 == 475
replace language = "Urdu" if g016 == 478
replace language = "Uzbek" if g016 == 479
replace language = "Venda" if g016 == 481
replace language = "Vietnamese" if g016 == 482
replace language = "Wolaytta" if g016 == 490
replace language = "Xhosa" if g016 == 493
replace language = "Yoruba" if g016 == 498
replace language = "Zulu" if g016 == 505

merge m:1 language using "data/data_chen2013_lang.dta"

drop if _merge ~= 3
drop _merge



*************************************
** Code and Clean Useful Variables **
*************************************

rename language LanguageAtHome

rename s002 Wave
rename s020 Year
rename s003 Country
rename a038 ImpChildQualSavings
rename a165 MostPeopCanBeTrusted
replace MostPeopCanBeTrusted = 0 if MostPeopCanBeTrusted == 2
rename f025 ReligDenomination
rename g017 BornWhere
rename g018 WhenCame
rename g026 MomImmigrant
rename g027 DadImmigrant
rename x001 Sex
rename x003 Age
gen AgeCat = 10
replace AgeCat = 20 if Age > 19
replace AgeCat = 30 if Age > 29
replace AgeCat = 40 if Age > 39
replace AgeCat = 50 if Age > 49
replace AgeCat = 60 if Age > 59
replace AgeCat = 70 if Age > 69
replace AgeCat = 80 if Age > 79
replace AgeCat = 90 if Age > 89
replace AgeCat = . if Age == .
rename x007 MaritalStatus
rename x011 NumChildren
rename x023 AgeFinSchool
rename x025 HighestEdu
rename x028 EmploymentStat
gen Unemployed = EmploymentStat == 7
gen FamSavedThisYr01 = 0
replace FamSavedThisYr01 = 1 if x044 == 1
replace FamSavedThisYr01 = . if x044 == .
* Makes a binary variable for Saved This Year *
rename x047 IncomeDecile
rename a001 FamilyImportant

keep g016 LanguageAtHome Wave Year Country ImpChildQualSavings MostPeopCanBeTrusted ReligDenomination BornWhere WhenCame MomImmigrant DadImmigrant Sex Age AgeCat MaritalStatus NumChildren AgeFinSchool HighestEdu EmploymentStat Unemployed FamSavedThisYr01 IncomeDecile any_ftr prediction_ftr inflectional_ftr family genus check FamilyImportant


* Remove Immigrants and Languages  for which data is not available *

drop if check == 1

replace BornWhere = 0 if BornWhere == .
drop if BornWhere > 1
drop if WhenCame ~= .
* drops immigrants *

rename any_ftr Any_FTR
rename prediction_ftr Prediction_FTR
rename inflectional_ftr Inflectional_FTR

* Brazilian Portuguese is known to be weak-FTR; change this in the data for cross-coutry regressions *
replace Prediction_FTR = 0 if Country == 76 & Language == "Portuguese"
replace Inflectional_FTR = 0 if Country == 76 & Language == "Portuguese"



***********************
* Generated Variables *
***********************

egen CountryTag = tag(Country) if FamSavedThisYr01 ~= .

* generate the market share (in your country) of your Language and FTR Class *

gen temp1 = .
replace temp1 = 1 if Prediction_FTR == 1
egen NumHighFTR = count(temp1), by(Country)
replace NumHighFTR = 0 if NumHighFTR == .
gen temp2 = 1 if Prediction_FTR == 0
egen NumLowFTR = count(temp2), by(Country)
replace NumLowFTR = 0 if NumLowFTR == .
egen NumFTROutliers = rowmin(NumHighFTR NumLowFTR)
tab NumFTROutliers if CountryTag
drop temp1 temp2

egen CountryGroup = group(Country)
egen CountryLangGroup = group(Country LanguageAtHome)

/*
gen NumInCountry = 0
gen temp1 = .
quietly forvalues Rho = 1/79 {
replace temp1 = 1 if CountryGroup == `Rho'
egen temp2 = count(temp1)
replace NumInCountry = temp2 if CountryGroup == `Rho'
replace temp1 = .
drop temp2
}

gen NumInCountryLang = 0
quietly forvalues Rho = 1/334 {
replace temp1 = 1 if CountryLangGroup == `Rho'
egen temp2 = count(temp1)
replace NumInCountryLang = temp2 if CountryLangGroup == `Rho'
replace temp1 = .
drop temp2
}
drop temp1
drop CountryGroup CountryLangGroup

gen HighFTRShare = NumHighFTR / NumInCountry
gen FTRShare = NumHighFTR / NumInCountry
replace FTRShare = NumLowFTR / NumInCountry if Prediction_FTR == 0

gen LanguageShare = NumInCountryLang / NumInCountry
sum NumInCountry NumInCountryLang LanguageShare FTRShare

*/


* Generate the Fixed Effects * 

egen FixEff0 = group(Sex AgeCat)
egen FixEff1 = group(Sex AgeCat Country Wave IncomeDecile HighestEdu)


*****************
** Regressions **
*****************


* Main Within-Country Table; Table 3 in the paper *

logit FamSavedThisYr01 Prediction_FTR i.FixEff0, vce(cluster Country)
eststo model1
vce2way logit FamSavedThisYr01 Prediction_FTR i.FixEff0, cluster (Country Year)
eststo model2
logit FamSavedThisYr01 Prediction_FTR i.FixEff0
eststo model3
boottest Prediction_FTR, cluster(Country Year) bootclust(Year)
scalar prob = r(p)
estadd scalar prob
meqrlogit FamSavedThisYr01 Prediction_FTR i.FixEff0 || LanguageAtHome:
eststo model4

clogit FamSavedThisYr01 Prediction_FTR, vce(cluster Country) group(FixEff1)
eststo model5
vce2way clogit FamSavedThisYr01 Prediction_FTR, cluster (Country Year) group(FixEff1)
eststo model6
clogit FamSavedThisYr01 Prediction_FTR, group(FixEff1)
eststo model7
boottest Prediction_FTR, cluster(Country Year) bootclust(Year) reps(10000)
scalar prob = r(p)
estadd scalar prob

preserve 
egen FixEff1_r = group(Sex AgeCat IncomeDecile HighestEdu)
by FixEff1_r, sort: egen mean_FamSavedThisYr01 = mean(FamSavedThisYr01)
drop if mean_FamSavedThisYr01==1 | mean_FamSavedThisYr01==0 
melogit FamSavedThisYr01 Prediction_FTR i.FixEff1_r i.Country i.Year || LanguageAtHome:
restore
eststo model8


esttab model* using table3.tex, b(3) se(3) scalar(prob) keep(Prediction_FTR) compress replace

